/**
 * 站点管理相关逻辑
 * @type {*}
 */
var pageObj = $('#page');
var pageSize = 50;
var cPage = 1;
var dataList;

//初始化
function init() {
    // page, pageSize
    loadData(cPage);
}

/**
 * 查询列表
 */
function searchList() {
    loadData(cPage);
}

function processPage(data) {
    // 处理分页
    pageObj.children().remove();
    pageObj.append('<div id="create_page_num"></div>');
    pageObj.find("#create_page_num").createPage({
        pageCount: data.pages,
        current: data.pageNum,
        backFn: function (p) {
            loadData(p, pageSize);
        }
    });
}

// 加载分页列表
function loadData(pageNo) {
    $.ajax({
        url: "/manage/site/list",
        type: 'POST',
        data: {
               pageNum: pageNo,
               pageSize: pageSize,
               name: $('#name').val(),
               createTimeStart:$('#createTimeStart').val(),
               createTimeEnd:$('#createTimeEnd').val()
        },
        success: function (data) {
            console.log(data);
            dataList = data.data.list;
            var allDataHtml = "";
            for (var i = 0; i < dataList.length; i++) {
                var cData = dataList[i];
                var cHtml =
                    "<tr data-id=\"" + cData.id + "\" data-no=\"" + cData.no + "\">\n" +
                    "<td>" + cData.id + "</td>\n" +
                    "<td>" + cData.name + "</td>\n" +
                    "<td>" + cData.url + "</td>\n" +
                    "<td>" + cData.remark + "</td>\n" +
                    "<td>" + cData.createTime + "</td>\n" +
                    "<td>" + cData.updateTime + "</td>\n" +
                    "<td>\n" +
                    "<a href=\"javascript:saveSite('" + i + "');\">修改</a>\n";
                    cHtml += "</td>\n" +
                    "</tr>";
                allDataHtml += cHtml;
            }
            $("#resultList").html();
            $("#resultList").html(allDataHtml);

            // 处理分页
            cPage = pageNo;
            processPage(data.data);
        }
    });
}

//保存站点
function saveSite(index) {
    var title ='站点修改';
    var siteInfo = null;
    if (null == index || "" == index) {
        title ='站点新建';
        siteInfo = {
            id:"",
            name:"",
            url:"",
            remark:""
        }
    }else{
        siteInfo = dataList[index];
    }

    var arr = '<div class="createTarget">' +
        '<ul>' +
        '<input type="hidden" id="cid" name="cid" value="'+siteInfo.id +'" />' +
        '<li>' +
        '<span>名称：</span>' +
        '<input type="text" id="cname" name="cname" value="'+siteInfo.name +'" />' +
        '</li>' +
        '<li>' +
        '<span>地址：</span>' +
        '<textarea rows="3" cols="20" name="curl" id="curl">'+siteInfo.url+'</textarea>' +
        '</li>' +
        '<li>' +
        '<span>备注：</span>' +
        '<textarea rows="3" cols="20" name="cremark" id="cremark">'+siteInfo.remark+'</textarea>' +
        '</li>' +
        '</ul>' +
        '</div>';

    var config = {
        title: title,
        body: arr,
        btn: [
            {
                title: '确定',
                className: 'button btn-ok btn-radius',
                dimiss: false,
                callback: function(){
                    return checkQualified();
                }
            },
            {
                title: '取消',
                className: 'button btn-blue-line btn-radius',
                dimiss: false,
                callback: function(){
                    return true;
                }
            }
        ]
    };

    createModalDom(config);
    $('#myModal').modal('show');
}

//检查输入的内容
function checkQualified() {
    var _id = $('#cid').val(),
        _name = $('#cname').val(),     //名称
        _url = $('#curl').val(),       //入口地址
        _remark = $('#cremark').val(); //网站来源

    $.ajax({
        url: "/manage/site/save",
        type: "post",
        data: {
            id: _id,
            name: _name,
            url: _url,
            remark: _remark
        },
        success: function(data){
            if (data.status) {
                if(_id == "" || _id == null) {
                    alert("站点新建成功");
                } else {
                    alert("站点修改成功");
                }
                loadData(cPage);
            } else {
                if(_id == "" || _id == null) {
                    alert("站点新建失败");
                } else {
                    alert("站点修改失败");
                }
            }
        },
        fail: function(data) {
            console.error("服务器内部错误");
        }
    });
    return true;
}


